package itman.elm.controller;

import itman.elm.po.User;
import itman.elm.service.UserService;
import itman.elm.service.impl.UserServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class UserController {

    // http://localhost:8080/elm/UserController/saveUser?userId=?&password=?&userName=?&userSex=?

    public Object saveUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        Integer userSex = Integer.valueOf(request.getParameter("userSex"));

        User user = new User(userId,password,userName,userSex,null);

        UserService service = new UserServiceImpl();
        int result = service.saveUser(user);
        return result;
    }

    // http://localhost:8080/elm/UserController/getUserByIdByPass?userId=?&password=?
    public Object getUserByIdByPass(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.getUserByIdByPass(userId, password);

        if(user!=null) {
            request.getSession().setAttribute("user", user);
        }
        return user;
    }

    // http://localhost:8080/elm/UserController/getUserById?userId=?
    public Object getUserById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        UserService service = new UserServiceImpl();
        User user = service.getUserById(userId);
        return user;
    }
}
